﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Globalization;

namespace jqGrid.Models
{
    public class ProductViewModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            ProductViewModel model = (ProductViewModel)base.BindModel(controllerContext, bindingContext);

            if (controllerContext.HttpContext.Request.Params["id"] != "_empty")
                model.ProductID = Convert.ToInt32(controllerContext.HttpContext.Request.Params["id"]);
            model.SupplierID = Convert.ToInt32(controllerContext.HttpContext.Request.Params["Supplier"]);
            model.CategoryID = Convert.ToInt32(controllerContext.HttpContext.Request.Params["Category"]);
            model.UnitPrice = Convert.ToDecimal(controllerContext.HttpContext.Request.Params["UnitPrice"].Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));

            return model;
        }
    }
}
